package cn.wetcland.es.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.wetcland.es.domain.EsClass;
import cn.wetcland.es.dto.StudentAddDTO;
import cn.wetcland.es.dto.StudentDeleteDTO;
import cn.wetcland.es.dto.StudentLoginDTO;
import cn.wetcland.es.dto.StudentUpdateDTO;
import cn.wetcland.es.mapper.EsClassMapper;
import cn.wetcland.es.utils.MyAssert;
import cn.wetcland.es.utils.bcrypt.BCrypt;
import cn.wetcland.es.utils.bcrypt.BCryptPasswordEncoder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.wetcland.es.domain.EsStudent;
import cn.wetcland.es.service.EsStudentService;
import cn.wetcland.es.mapper.EsStudentMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
* @author Wetcland
* @description 针对表【es_student】的数据库操作Service实现
* @createDate 2023-04-29 17:04:49
*/
@Service
public class EsStudentServiceImpl extends ServiceImpl<EsStudentMapper, EsStudent> implements EsStudentService{

    @Resource
    private EsStudentMapper studentMapper;

    @Resource
    private EsClassMapper classMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addNewStudent(StudentAddDTO studentAddDTO) {
        MyAssert.isTrue(studentAddDTO.getClassId() != 0, "未选择班级ID");
        MyAssert.isTrue(NumberUtil.isNumber(studentAddDTO.getSno()), "学号必须由数字组成");
        MyAssert.notBlank(studentAddDTO.getName(), "姓名不能为空");
        MyAssert.isTrue(studentAddDTO.getGender() == 1 || studentAddDTO.getGender() == 2, "请选择性别");
        MyAssert.notNull(classMapper.selectById(studentAddDTO.getClassId()), "该班级不存在");
        EsStudent student = new EsStudent();
        student.setEsStudentClassId(studentAddDTO.getClassId());
        student.setEsStudentSno(studentAddDTO.getSno());
        student.setEsStudentName(studentAddDTO.getName());
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        student.setEsStudentPassword(passwordEncoder.encode("123456"));
        student.setEsStudentGender(studentAddDTO.getGender());
        studentMapper.insert(student);
    }

    @Override
    public List<EsStudent> getAllStudent(Long classId) {
        MyAssert.notNull(classMapper.selectById(classId), "该班级不存在");
        return studentMapper.selectList(new QueryWrapper<EsStudent>().eq("es_student_class_id", classId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStudent(StudentUpdateDTO studentUpdateDTO) {
        MyAssert.isTrue(studentUpdateDTO.getStudentId() != 0, "未选择学生ID");
        MyAssert.isTrue(NumberUtil.isNumber(studentUpdateDTO.getSno()), "学号必须由数字组成");
        MyAssert.notBlank(studentUpdateDTO.getName(), "姓名不能为空");
        MyAssert.isTrue(studentUpdateDTO.getGender() == 1 || studentUpdateDTO.getGender() == 2, "请选择性别");
        EsStudent esStudent = studentMapper.selectById(studentUpdateDTO.getStudentId());
        MyAssert.notNull(esStudent, "该学生不存在");
        esStudent.setEsStudentSno(studentUpdateDTO.getSno());
        esStudent.setEsStudentName(studentUpdateDTO.getName());
        esStudent.setEsStudentGender(studentUpdateDTO.getGender());
        studentMapper.updateById(esStudent);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteStudent(StudentDeleteDTO studentDeleteDTO) {
        MyAssert.isTrue(studentDeleteDTO.getClassId() != 0, "未选择班级ID");
        MyAssert.isTrue(studentDeleteDTO.getStudentId() != 0, "未选择学生ID");
        EsStudent esStudent = studentMapper.selectById(studentDeleteDTO.getStudentId());
        MyAssert.notNull(esStudent, "该学生不存在");
        studentMapper.deleteById(studentDeleteDTO.getStudentId());
    }

    @Override
    public void login(StudentLoginDTO loginDTO, HttpSession session) {
        MyAssert.notBlank(loginDTO.getStudentSno(), "学号不能为空");
        MyAssert.notBlank(loginDTO.getPassword(), "密码不能为空");
        EsStudent student = studentMapper.selectOne(new QueryWrapper<EsStudent>().eq("es_student_sno", loginDTO.getStudentSno()));
        MyAssert.notNull(student, "未查询到该学号的学生");
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        MyAssert.isTrue(passwordEncoder.matches(loginDTO.getPassword(), student.getEsStudentPassword()), "密码错误");
        session.setAttribute("roleName", "student");
        session.setAttribute("target", student);
    }
}




